#ifndef _REPLAY_PROC_H
#define _REPLAY_PROC_H

#include "rec_read.h"
#include "rec_read_base.h"
#include <map>

using namespace std;

typedef enum _read_status_
{
	READ_STATUS_START,
	READ_STATUS_STOP,
	READ_STATUS_READING,
	READ_STATUS_SLEEP,
}READ_STATUS;

class CReplay1CHProc
{
public:
	CReplay1CHProc(int clientID = 0,int channel = 0);
	virtual ~CReplay1CHProc();
	typedef struct _SEARCH_INFO_
	{
		int		diskIndex;
		int		fileIndex;
		int		recSeg;
		int64_t tmStart;
		int64_t tmEnd;
	}SEARCH_INFO;

	int Init(REC_FILE_LIST & fileList,int64_t startTime, int64_t endTime, int64_t seekTime,int recType = -1);
	int InitDownloadFile(FILE_INFO fileInfo);
	int StartRead();

	void StopRead();

	int GetFrameData(uchar_t* frameBuf,int bufSize);
	int ReadGPSInfo(uint32_t gpsTm,uchar_t * gpsInfo,int gpsInfoSize);

	int SeekToTime(int64_t timeMs);
	int SetPlayMode(PLAYBACK_MODE playMode,int speed,bool bReSeek = true);

	void Reset();
	void ClearFIFOBuf();
	int64_t GetLastFrmTime();
	int  GetRecordFileList(REC_FILE_LIST & fileList,int64_t startTime, int64_t endTime);
	PLAYBACK_MODE GetPlayMode()
	{
		return m_playMode;
	}
	void SetClientCH(int clientID, int channel)
	{
		m_clientID = clientID;
		m_channel  = channel;
	}
	void SetChannel(int ch)
	{
		m_channel = ch;
	}

	int64_t GetStartTime()
	{
		return m_startTime;
	}

 	void SetStartTime(int64_t startTime)
	{
		m_curFileStartTime = m_startTime = startTime;
	}

	bool IsReading()
	{
		if(m_pReader)
		{
			return m_pReader->IsOpen();
		}
		else
			return false;
	}

	REC_FILE_LIST* GetFileList()
	{
		return & m_lstFile;
	}
	int GetChannel()
	{
		return m_channel;
	}

	int GetClientID()
	{
		return m_clientID;
	}
	void resetFileLst()
	{
		m_vecFile.clear();
		m_curIndex =0;
	}
	int GetRecNum()
	{
		return (int)m_vecFile.size();
	}
	void  SeekToNextFile (bool bForward = true);
	void  PrintfRecFile(int curIndex = -1);
	int   ReadFrameData(uchar_t * frmBuf,int bufSize);
	int   ReplayWait(int64_t curTm);
	int   FifoClear();
	int GetCurFileTimeSeg(int64_t & tmStart,int64_t & tmEnd);
protected:
	int SeekToFileByTime(int64_t curTime);
	int FindRecord(int64_t curTime);

protected:
	REC_FILE_LIST			m_lstFile;
	vector<REC_FILE_INFO>   m_vecFile;
	int						m_curIndex;

	REC_FILE_INFO*			m_pFinfo;
	//CDiskInfoSet *			m_pDiskSet;
	//CIndexSet	 *          m_pIndexSet;

	int						m_clientID;
	int						m_channel;
	int						m_reChkCnt;

	int64_t					m_startTime;
	int64_t                 m_endTime;
	int64_t					m_curFileStartTime;
	int64_t                 m_curFileEndTime;
	int						m_recType;

	PLAYBACK_MODE			m_playMode;
	CRecReadBase*			m_pReader;
	CCross_Lock				m_proc;

};

typedef map<int, CReplay1CHProc*> MP_REPLAY_CH_T;
typedef vector<CReplay1CHProc*>   VEC_REPLAY_CH_T;

#define END_REPLAYFILE_INDEX      -1
#endif //_REPLAY_PROC_H
